//
//  PhotoVC.m
//  first
//
//  Created by 毕小强 on 16/12/12.
//  Copyright © 2016年 毕小强. All rights reserved.
//

#import "PhotoVC.h"

@interface PhotoVC ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIActionSheetDelegate>

@end

@implementation PhotoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"选择方式" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相机",@"相册", nil];
    [actionSheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        [self cameraEvent];
    }
    if (buttonIndex == 1) {
        [self photoEvent];
    }
}
-(void)cameraEvent{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        UIView *selectView = [[UIView alloc]initWithFrame:CGRectMake(0, 100, kScreenWidth, kScreenWidth)];
        selectView.layer.borderColor = [UIColor whiteColor].CGColor;
        selectView.layer.borderWidth = 1.0;
        selectView.backgroundColor = [UIColor clearColor];
        [picker.view addSubview:selectView];
        
        [self presentViewController:picker animated:YES completion:nil];
    }else{
        //如果没有提示用户
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"你没有摄像头" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alert show];
    }
}
-(void)photoEvent{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:picker animated:YES completion:nil];
    }else{
        //如果没有提示用户
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"打开相册失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alert show];
    }
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    
    CGFloat height = 200/image.size.width*image.size.height;
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 64,200, height)];
    imageView.image = image;
    [self.view addSubview:imageView];
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    NSData *fData = UIImageJPEGRepresentation(image, 1.0);
    NSData *eData = UIImageJPEGRepresentation(image, 0.5);
    NSLog(@"%ld",fData.length);
    NSLog(@"%ld",eData.length);
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
